﻿

namespace Mulala.Cavan.Application.MainBoundedContext.InformationModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Application.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.Seedwork;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Application.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Application.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
using Mulala.Cavan.Application.MainBoundedContext.SecurityModule;

    public class UserMessageAppService : BaseAppService<UserMessage, UserMessageSearch, IUserMessageRepository>, IUserMessageAppService
    {
        private ICorporationAppService CorporationAppService { get; set; }
        private INannyAppService NannyAppService { get; set; }
        private IProprietorAppService ProprietorAppService { get; set; }
        private IUserAccountAppService UserAccountAppService { get; set; }

        private IApplicationContext SpringContext = null;

        public override BoolGenericResult<UserMessage> Add(UserMessage userMessage)
        {
            this.SpringContext = ContextRegistry.GetContext();

            BoolGenericResult<UserMessage> result = new BoolGenericResult<UserMessage>();
            this.UserAccountAppService = this.SpringContext.GetObject(MulalaAppServiceConst.USERACCOUNTAPPSERVICE) as IUserAccountAppService;
            UserAccount ownerUser = this.UserAccountAppService.GetById(userMessage.OwnerUser.ID);

            if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Corporation.GetHashCode())
            {
                this.CorporationAppService = this.SpringContext.GetObject(MulalaAppServiceConst.CORPORATIONAPPSERVICE) as ICorporationAppService;
                Corporation corporation = this.CorporationAppService.GetById(userMessage.MessagerUser.ID);
                result = corporation.LeaveMessage(ownerUser, userMessage.Content);
            }
            else if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Nanny.GetHashCode())
            {
                this.NannyAppService = this.SpringContext.GetObject(MulalaAppServiceConst.NANNYAPPSERVICE) as INannyAppService;
                Nanny nanny = this.NannyAppService.GetById(userMessage.MessagerUser.ID);
                result = nanny.LeaveMessage(ownerUser, userMessage.Content);
            }
            else if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Proprietor.GetHashCode())
            {
                this.ProprietorAppService = this.SpringContext.GetObject(MulalaAppServiceConst.PROPRIETORAPPSERVICE) as IProprietorAppService;
                Proprietor proprietor = this.ProprietorAppService.GetById(userMessage.MessagerUser.ID);
                result = proprietor.LeaveMessage(ownerUser, userMessage.Content);
            }

            if (result.IsSuccess)
            {
                this.Repository.Save(result.Object);
            }

            return result;
        }

        public BoolGenericResult<UserMessage> AddUserMessageReply(UserMessage userMessage)
        {
            this.SpringContext = ContextRegistry.GetContext();
            
            BoolGenericResult<UserMessage> result = new BoolGenericResult<UserMessage>();

            UserMessage repliedMessage = this.Repository.Get(userMessage.ParentID);
            if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Corporation.GetHashCode())
            {
                this.CorporationAppService = this.SpringContext.GetObject(MulalaAppServiceConst.CORPORATIONAPPSERVICE) as ICorporationAppService;
                Corporation corporation = this.CorporationAppService.GetById(userMessage.MessagerUser.ID);
                result = corporation.ReplyMessage(repliedMessage, userMessage.Content);
            }
            else if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Nanny.GetHashCode())
            {
                this.NannyAppService = this.SpringContext.GetObject(MulalaAppServiceConst.NANNYAPPSERVICE) as INannyAppService;
                Nanny nanny = this.NannyAppService.GetById(userMessage.MessagerUser.ID);
                result = nanny.ReplyMessage(repliedMessage, userMessage.Content);
            }
            else if (userMessage.MessagerUser.Roles.FirstOrDefault().ID == SecurityIdentifier.Role.Proprietor.GetHashCode())
            {
                this.ProprietorAppService = this.SpringContext.GetObject(MulalaAppServiceConst.PROPRIETORAPPSERVICE) as IProprietorAppService;
                Proprietor proprietor = this.ProprietorAppService.GetById(userMessage.MessagerUser.ID);
                result = proprietor.ReplyMessage(repliedMessage, userMessage.Content);
            }
            if (result.IsSuccess)
            {
                this.Repository.Update(repliedMessage);
                this.Repository.Save(result.Object);
            }

            return result;
        }
    }
}
